package com.zis.auction.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zis.auction.model.AuctBid;
import com.zis.auction.model.SubFile;
import com.zis.auction.service.IAuctBidInter;
import com.zis.auction.service.ISubFileInter;
import com.zis.core.ResponseEntity;
import com.zis.platform.model.Dictionary;
import com.zis.platform.service.IDictionaryInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;


/**
 * Create by wangshen 2018/1/8
 */
@RestController
@RequestMapping("/api/open/auction/bid")
public class AuctBidRestful {


    @Autowired
    private IAuctBidInter auctBidInter;

    @Autowired
    private IDictionaryInter dictionaryInter;

    @Autowired
    private ISubFileInter subFileInter;

    /**
     * 获取微信首页的数据
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    @ResponseBody
    public String pageList() throws Exception {
        return auctBidInter.findIndex();
    }

    @RequestMapping(value = "/list/all", method = RequestMethod.GET)
    @ResponseBody
    public String findAll(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize) throws Exception {
        return auctBidInter.findAll(page, pageSize);
    }

    @RequestMapping(value = "/list/suggest", method = RequestMethod.GET)
    @ResponseBody
    public String findBySuggest(@RequestParam("suggest") String suggest,
                                @RequestParam("page") Integer page,
                                @RequestParam("pageSize") Integer pageSize) {
        return auctBidInter.findBySuggest(suggest, page, pageSize);
    }

    /**
     * 主键查询
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/select/{id}", method = RequestMethod.GET)
    @ResponseBody
    public AuctBid findById(@PathVariable("id") String id) throws Exception {
        return auctBidInter.selectById(id);
    }

    /**
     * 获取标的分类信息
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/type/options", method = RequestMethod.GET)
    @ResponseBody
    public String typeOptions() throws Exception {
        List<Dictionary> dictList = dictionaryInter.findByTypeCode("tender");
        if(dictList!=null&&dictList.size()>0){
            LinkedList list = new LinkedList();
            JSONObject dictJSON = new JSONObject();
            dictJSON.put("label", "全部");
            dictJSON.put("value", "recom");
            list.add(dictJSON);
            for (Dictionary dict : dictList) {
                dictJSON = new JSONObject();
                dictJSON.put("label", dict.getDictName());
                dictJSON.put("value", dict.getDictValue());
                list.add(dictJSON);
            }
            return ResponseEntity.createNormalJsonResponse(JSON.parseArray(JSON.toJSONString(list)));
        }
        return ResponseEntity.createNULLJsonResponse("未获取分类信息");
    }

    @RequestMapping(value = "/hits/{abdId}")
    @ResponseBody
    public String addHits(@PathVariable("abdId") String abdId) throws Exception{
        return auctBidInter.updateHits(abdId);
    }


    @RequestMapping(value = "/file/{bussId}")
    @ResponseBody
    public List<SubFile> findByBussId(@PathVariable("bussId") String bussId) throws Exception {
        return subFileInter.findByBussId(bussId);
    }

}
